import { defineStore } from "pinia";

import {getSystemInfo,putSystemInfo} from '@/api/system';
import Message from "@/utils/message";

export const useSystem = defineStore('system', {
	state: () => {
		return {
			systemInfo: {},
			systemNotes: {},
			systemUpdate: {},
		}
	},
	actions: {
		setLogoTitle() {
			let link: any = document.querySelector("link[rel='icon']");
			if (!link) {
				link = document.createElement("link");
				link.rel = "icon";
				document.getElementsByTagName("head")[0].appendChild(link);
			}
			link.href = this.systemInfo['logo'] || '重医问诊系统';
			let ProjectTitle: any = document.querySelector("title");
			if (!ProjectTitle) {
				ProjectTitle = document.createElement("title");
				document.getElementsByTagName("head")[0].appendChild(ProjectTitle);
			}
			ProjectTitle.innerHTML = this.systemInfo['system_name'];
		},
		async getSystem(isRead = false) {      // 获取信息
			if (isRead || !this.systemInfo.system_name) {
				const result = await getSystemInfo();
				if (!result) return;
				const { res } = result || {};
				// if (res) {
				// 	res.forEach((item: any) => {
				// 		this.systemInfo[item.key] = item.value;
				// 		this.systemNotes[item.key] = item.title;
				// 		this.systemUpdate[item.key] = item.update;
				// 	});
				// }
			}
			// this.setLogoTitle();
		},
		async updateSystem(data) {
				const result = await putSystemInfo(data);
				if (!result) return Message.error('修改失败');
				Message.success('修改成功');
				this.systemInfo[data.key] = data.value;
				this.setLogoTitle();
		},
		closeSystem(){
			return this.systemInfo;
		}
	},
	getters: {

	},
	// persist: true
})
